const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      Enterprise_Info_SerialNumber: $.cookie("enterprise"),
      pickerOptions: pickerOptions,
      tableData: [],
      rules: {
        App_ID: [{
          required: true,
          message: '请输入小程序AppID',
          trigger: 'blur',
        }],
        App_Name: [{
          required: true,
          message: '请输入小程序名称',
          trigger: 'blur',
        }]
      },
      fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        App_ID: '',
        Public_Id:'',

        App_Name: '',
        App_Secret: '',
        Public_Secret:'',
        Children: {
          enterprise_appid_detail: [
            {
              Enterprise_Info_SerialNumber: '',
              mchid: '',
              App_Style_SerialNumber: '',
              Enterprise_Appid_Detail_SerialNumber: '',
              App_ID: '',
            }
          ],
        }
      },
      loading: false,
      list: [],
      formData: {
        Primary_Key_Name: "Enterprise_Appid_SerialNumber",
        Primary_Key: "",
        Data: "enterprise_appid",
        enterpriseData: [],
      },
      mchidData: [],
      enterpriseData: [],
    }
  },
  mounted() {
    const Primary_Key = sessionStorage.getItem('App_ID');
    this.getShopName();
    this.getList();
    this.get_original_value(Primary_Key);
    console.log('app1', this.form.App_ID);
    this.getMchid();
    this.getEnterprise();
  },
  methods: {
    // 获取门店
    getShopName() {
      let _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + "/getInquiryAction",
        data: {
          Data: "store_info",
          token: $.cookie("token")
        },
        success: function (data) {
          data = data.filter(function (item) {
            return item.Row_Status != '1'
          })
          _this.tableData = data;
        },
        dataType: "json"
      });
    },
    //查询所有企业
    getEnterprise() {
      let _this = this
      $.ajax({
        type: 'GET',
        url: DomainName + '/getEnterpriseInfoAction?token=' + $.cookie('token'),
        success: function (data) {
          _this.enterpriseData = data?.Data;
          console.log(data, 'enterpriseData', _this.enterpriseData)
        },
        dataType: "json"
      });
    },
    // 查询商户号
    getMchid() {
      let _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + '/getInquiryAction?token=' + $.cookie('token'),
        data: {
          Data: "wechatpay",
          isNullEnterprise: true
        },
        success: function (data) {
          console.log('data', data);
          _this.mchidData = data;
          console.log('mchidData', _this.mchidData) 
        },
        dataType: "json"
      });
    },
    //查原值
    get_original_value(App_ID) {
      let _this = this
      console.log('app2', App_ID);
      $.ajax({
        url: DomainName + '/getSubtableInquiryOriginal?token=' + $.cookie('token'),
        type: 'POST',
        data: {
          JSONObject: JSON.stringify({ enterprise_appid: { enterprise_appid_detail: {} } }),
          Primary_Key: App_ID
        },
        success: function (res) {
          _this.form = res.enterprise_appid[0];
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getList() {
      let _this = this
      $.ajax({
        url: DomainName + '/getInquiryAction?token=' + $.cookie('token'),
        type: 'POST',
        data: {
          Data: "enterprise_appid_style",
        },
        success: function (res) {
          _this.list = res
          console.log(_this.list);
          console.log(res);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { add, modify } = _this.deal()
          let data = {}
          if (add == null) {
            data = { Data: JSON.stringify({ Modify: { enterprise_appid: [modify] } }), token: $.cookie("token") }
          } else {
            data = {
              Data: JSON.stringify(
                {
                  Modify: { enterprise_appid: [modify] },
                  Add: { enterprise_appid: [add] }
                }
              ),
              token: $.cookie("token")
            }
          }
          console.log(JSON.parse(data.Data));
          _this.loading = true
          $.ajax({
            url: DomainName + '/getEditAction?token=' + $.cookie('token'),
            type: 'POST',
            data,
            success: function (res) {
              if (res.Result == "成功") {
                _this.$message({
                  message: '修改成功',
                  type: 'success'
                });
                window.history.go(-1);
                // window.location.href = './Applet.html'
                // window.location.href = 'index.html';
              } else {
                _this.$message.error(res.Result);
              }
            },
            error: function (error) {
              console.log(error)
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        }
        else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    deal() {
      const add = deepClone(this.form)
      console.log('add', add);
      const modify = deepClone(this.form)
      console.log('mo', modify);
      let add_enterprise_appid_detail = [], modify_enterprise_appid_detail = []
      for (const i in modify.Children.enterprise_appid_detail) {
        const obj = modify.Children.enterprise_appid_detail[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_enterprise_appid_detail.push(obj)
        } else {
          modify_enterprise_appid_detail.push(obj)
        }
      }

      add.Children.enterprise_appid_detail = add_enterprise_appid_detail

      modify.Children.enterprise_appid_detail = modify_enterprise_appid_detail
      if (add_enterprise_appid_detail.length == 0) {
        return { add: null, modify };
      }
      return { add, modify };
    },
    editMember() {
      console.log(1);
      this.form.Children.enterprise_appid_detail.push({
        Enterprise_Info_SerialNumber: '',
        mchid: '',
        App_Style_SerialNumber: '',
        Enterprise_Appid_Detail_SerialNumber: '',
        App_ID: this.form.App_ID
      });
    },
    deletMember() {
      if (this.form.Children.enterprise_appid_detail.filter(item => item.Row_Status != '1').length > 1) {
        let i = this.form.Children.enterprise_appid_detail.filter(item => item.Row_Status != '1').length - 1
        let obj = this.form.Children.enterprise_appid_detail.filter(item => item.Row_Status != '1')[i]
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          this.form.Children.enterprise_appid_detail.pop()
        } else {
          this.$confirm('警告！此操作将删除一条权限标识,可能会引起页面问题, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(() => {
            this.form.Children.enterprise_appid_detail.filter(item => item.Row_Status != '1')[i].Row_Status = '1'
          }).catch(() => {
            this.$message({
              type: 'warning',
              message: '已取消删除'
            });
          });
        }
      }else{
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    }
  }
})